package lib

import (
	"encoding/json"
	"fmt"
	jpushclient "github.com/ylywyn/jpush-api-go-client"
	"io/ioutil"
	"os"
)

type PushServer struct {
	Secret string `json:"secret"` //账号账户
	AppKey string `json:"appKey"` //授权唯一ID
}

var pushServer PushServer

/*
 描述 ： 推送消息
 ******************************************************************************/
func (this *PushServer) DoPush(ids []string, Contact string) {
	var pf jpushclient.Platform
	pf.Add(jpushclient.ANDROID)
	pf.Add(jpushclient.IOS)
	pf.Add(jpushclient.WINPHONE)
	pf.All()
	//构建接收听众： jpushclient.Audience
	var ad jpushclient.Audience
	ad.SetID(ids)
	//构建通知 jpushclient.Notice，或者消息： jpushclient.Message
	var notice jpushclient.Notice
	notice.SetAlert("alert_test")
	notice.SetAndroidNotice(&jpushclient.AndroidNotice{Alert: Contact})
	notice.SetIOSNotice(&jpushclient.IOSNotice{Alert: Contact})
	notice.SetWinPhoneNotice(&jpushclient.WinPhoneNotice{Alert: Contact})
	var msg jpushclient.Message
	msg.Title = "Hello"
	msg.Content = "你是ylywn"
	//  构建jpushclient.PayLoad
	payload := jpushclient.NewPushPayLoad()
	payload.SetPlatform(&pf)
	payload.SetAudience(&ad)
	payload.SetMessage(&msg)
	payload.SetNotice(&notice)
	bytes, _ := payload.ToBytes()
	fmt.Printf("%s\r\n", string(bytes))
	//  构建PushClient，发出推送
	c := jpushclient.NewPushClient(pushServer.Secret, pushServer.AppKey)
	var str, _ = c.Send(bytes)
	fmt.Printf("ok:%s", str)
}

func init() {
	jsonFile, err := os.Open("./config/push.json")
	if err != nil {
		panic("打开文件错误，请查看:" + err.Error())
	}
	defer jsonFile.Close()
	data, err := ioutil.ReadAll(jsonFile)
	json.Unmarshal(data, &pushServer)
	fmt.Println("推送消息", pushServer)

}
